package great.easychat.help.viewModel

import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.pince.frame.mvvm.architecture.BaseViewModel
import great.easychat.help.bean.XiaobaiBean
import great.easychat.help.bean.XiaobaiListBean
import java.lang.reflect.Type

public open class AppBaseViewModel(application: Application, data: Bundle?) : BaseViewModel(application, data) {

    fun checkXiaobai(response: String): Int {
        if (TextUtils.isEmpty(response)) {
            return -1
        }
        val xiaobaiBean: XiaobaiBean = JSON.parseObject(response, XiaobaiBean::class.java)
        if (xiaobaiBean.ret == 200) {
            return xiaobaiBean.data.err_code
        } else {
            return xiaobaiBean.ret
        }
    }

    fun checkXiaobaiObject(response: String): Pair<Int, String> {
        if (TextUtils.isEmpty(response)) {
            return Pair<Int, String>(-1, "")
        }
        val xiaobaiBean: XiaobaiBean = JSON.parseObject(response, XiaobaiBean::class.java)
        if (xiaobaiBean.ret == 200) {
            return Pair<Int, String>(xiaobaiBean.data.err_code, xiaobaiBean.data.data)
        } else {
            return Pair<Int, String>(xiaobaiBean.ret, "")
        }
    }

    fun <T> checkXiaobaiList(response: String, clazz: Class<T>): Pair<Int, List<T>?> {
        if (TextUtils.isEmpty(response)) {
            return Pair<Int, List<T>>(-1, emptyList())
        }
        val xiaobaiListBean: XiaobaiListBean<T> = JSON.parseObject(response, object : TypeReference<XiaobaiListBean<T>>(clazz) {})!!
        if (xiaobaiListBean.ret == 200) {
            return Pair<Int, List<T>>(xiaobaiListBean.data.err_code, xiaobaiListBean.data.list)
        } else {
            return Pair<Int, List<T>>(xiaobaiListBean.data.err_code, emptyList())
        }
    }

}
